package com.jingdianjichi.subject.application.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 网关拦截器 - 微服务上下文打通
 * @author 86156
 * @version 1.0
 * @date 2024-11-11 9:57
 */
public class FeignRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes
                = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        if(ObjectUtils.isNotEmpty(request)){
            String loginId = request.getHeader("loginId");
            if(StringUtils.isNotBlank(loginId)){
                requestTemplate.header("loginId", loginId);
            }
        }
    }
}
